package com.loongtech.bi.action.report;

import com.loongtech.bi.manager.realTime.BossRealTimeManager;
import com.loongtech.bi.manager.report.GameReportManager;
import com.loongtech.bi.manager.report.GameReportManager.GameReportData;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleChannelManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 运营报表-平台/渠道/区服Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class GameReportAction {
    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private String selectedPlatform = "0";  // 0=全部平台;2=ios正版;1=ios越狱;3=安卓
    private String selectedServer = "all";  // all=全部平台;ioszb=ios正版;hf=混服;yyb=应用宝;yhlm=硬核联盟

    private List<GameReportData> platformDataList;  // 平台数据，包括平台汇总和平台详细，其中平台详细是平台汇总里GameReportData对象的togglerDatas属性。平台汇总数据也用于供前端显示
	private List<GameReportData> detailChannelDataList;  // 详细渠道数据
    private List<GameReportData> detailServerDataList;  // 详细区服数据

    private List<GameReportData> detailPlatformDisplayData;  // 供前端显示的平台详细数据
    private List<GameReportData> channelDisplayDataList;  // 供前端显示的渠道汇总数据
    private List<GameReportData> detailChannelDisplayDataList;  // 供前端显示的渠道详细数据
    private List<GameReportData> detailServerDisplayDataList;  // 供前端显示的区服详细数据

    private List<GameReportData> cpsDataList;  // 供前端cps汇总数据

	@Autowired
	GameReportManager gameReportManager;

	@Autowired
    SysRoleChannelManager sysRoleChannelManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "查询运营报表-平台/渠道/区服");

        platformDataList = new ArrayList<>();
        detailChannelDataList = new ArrayList<>();
        detailServerDataList = new ArrayList<>();
        detailPlatformDisplayData = new ArrayList<>();
        channelDisplayDataList = new ArrayList<>();
        detailChannelDisplayDataList = new ArrayList<>();
        detailServerDisplayDataList = new ArrayList<>();
        cpsDataList = new ArrayList<>();

        Date startDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio) + 1) * Time.DAY))).getTime()));  // 开始时间
        Date endDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()));  // 结束时间

        List<GameReportData> allData = gameReportManager.getAllGameReportData(startDay, endDay);
		if (allData != null && allData.size() > 0) {
			gameReportManager.updateData(allData, platformDataList, detailChannelDataList, detailServerDataList);
            selectPlatform();
            selectServer();
        }

        getChannelDatasByUser();
	}

    public void getChannelDatasByUser() {
        cpsDataList = new ArrayList<>();
        List<GameReportData> allList = getChannelDisplayDataList();
        Set<String> selectedSet = sysRoleChannelManager.getChannelSetByUserId(session.getUserId());
        if (null == selectedSet || selectedSet.isEmpty() || null == allList || allList.isEmpty()) {
            return;
        }

        for (GameReportData info : allList) {
            if (selectedSet.contains(info.getChannelId())) {
                cpsDataList.add(info);
            }
        }
    }
	
    public void selectPlatform() {
        detailPlatformDisplayData.clear();
        channelDisplayDataList.clear();
        detailChannelDisplayDataList.clear();

        for (GameReportData p: platformDataList) {  // 取对应平台的平台详细数据
            if (selectedPlatform.equals(p.getPlatformId())) {
                detailPlatformDisplayData.addAll(p.getTogglerDatas());
                break;
            }
        }

        for (GameReportData p: detailChannelDataList) {  // 取对应平台的渠道汇总和详细数据
            if ("0".equals(selectedPlatform)) {
                channelDisplayDataList.add(p);
                detailChannelDisplayDataList.addAll(p.getTogglerDatas());
            } else {
                if (selectedPlatform.equals(p.getPlatformId())) {
                    channelDisplayDataList.add(p);
                    detailChannelDisplayDataList.addAll(p.getTogglerDatas());
                }
            }
        }

        getChannelDatasByUser();
    }

    public void selectServer() {
        detailServerDisplayDataList.clear();

        for (GameReportData p: detailServerDataList) {
            if ("all".equals(selectedServer)) {
                detailServerDisplayDataList.addAll(p.getTogglerDatas());
            } else {
                if (p.getServerTypeName().equals(BossRealTimeManager.INDEX2NAME.get(selectedServer))) {
                    detailServerDisplayDataList.addAll(p.getTogglerDatas());
                }
            }
        }
    }

    /**
     * 切换天数
     */
    public void radioChange() {
        int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
        dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));

        search();
    }

    /**
     * 划动天数
     */
    public void spinnerChange() {
        search();
    }
	
	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public List<GameReportData> getPlatformDataList() {
        return platformDataList;
    }

    public void setPlatformDataList(List<GameReportData> platformDataList) {
        this.platformDataList = platformDataList;
    }

    public List<GameReportData> getDetailChannelDataList() {
        return detailChannelDataList;
    }

    public void setDetailChannelDataList(List<GameReportData> detailChannelDataList) {
        this.detailChannelDataList = detailChannelDataList;
    }

    public String getSelectedPlatform() {
        return selectedPlatform;
    }

    public void setSelectedPlatform(String selectedPlatform) {
        this.selectedPlatform = selectedPlatform;
    }

    public List<GameReportData> getDetailPlatformDisplayData() {
        return detailPlatformDisplayData;
    }

    public void setDetailPlatformDisplayData(List<GameReportData> detailPlatformDisplayData) {
        this.detailPlatformDisplayData = detailPlatformDisplayData;
    }

    public List<GameReportData> getDetailChannelDisplayDataList() {
        return detailChannelDisplayDataList;
    }

    public void setDetailChannelDisplayDataList(List<GameReportData> detailChannelDisplayDataList) {
        this.detailChannelDisplayDataList = detailChannelDisplayDataList;
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public List<GameReportData> getChannelDisplayDataList() {
        return channelDisplayDataList;
    }

    public void setChannelDisplayDataList(List<GameReportData> channelDisplayDataList) {
        this.channelDisplayDataList = channelDisplayDataList;
    }

    public List<GameReportData> getDetailServerDataList() {
        return detailServerDataList;
    }

    public void setDetailServerDataList(List<GameReportData> detailServerDataList) {
        this.detailServerDataList = detailServerDataList;
    }

    public List<GameReportData> getDetailServerDisplayDataList() {
        return detailServerDisplayDataList;
    }

    public void setDetailServerDisplayDataList(List<GameReportData> detailServerDisplayDataList) {
        this.detailServerDisplayDataList = detailServerDisplayDataList;
    }

    public String getSelectedServer() {
        return selectedServer;
    }

    public void setSelectedServer(String selectedServer) {
        this.selectedServer = selectedServer;
    }

    public List<GameReportData> getCpsDataList() {
        return cpsDataList;
    }

    public void setCpsDataList(List<GameReportData> cpsDataList) {
        this.cpsDataList = cpsDataList;
    }
}
